package com.zkdcloud.service;

import com.zkdcloud.domain.Business;
import com.zkdcloud.domain.Favorite;
import com.zkdcloud.dto.FavoriteDisplay;
import com.zkdcloud.repository.BusinessDao;
import com.zkdcloud.repository.FavoriteDao;
import com.zkdcloud.util.RandomUtil;
import org.jfaster.mango.plugin.page.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 商家service
 *
 * @author zk
 * @since 2018/5/2
 */
@Service
public class BusinessService {
    /**
     * static logger
     */
    private static Logger logger = LoggerFactory.getLogger(BusinessService.class);
    /**
     * businessDao
     */
    @Autowired
    private BusinessDao businessDao;
    /**
     * 喜欢dao
     */
    @Autowired
    private FavoriteDao favoriteDao;

    /**
     * 添加business
     *
     * @param business 商家
     */
    public void add(Business business){
        if(StringUtils.isEmpty(business.getId())){
            business.setId(RandomUtil.getUUID());
        }
        if(StringUtils.isEmpty(business.getName())){
            business.setName("");
        }
        if(StringUtils.isEmpty(business.getAddress())){
            business.setAddress("");
        }
        if(StringUtils.isEmpty(business.getBookingTimes())){
            business.setBookingTimes("");
        }
        if(StringUtils.isEmpty(business.getBulletin())){
            business.setBulletin("");
        }
        if(StringUtils.isEmpty(business.getCategory())){
            business.setCategory(1);
        }
        if(StringUtils.isEmpty(business.getCreateTime())){
            business.setCreateTime(System.currentTimeMillis());
        }
        if(StringUtils.isEmpty(business.getMinPrice())){
            business.setMinPrice(0.0);
        }
        if(StringUtils.isEmpty(business.getMonthSales())){
            business.setMonthSales(0);
        }
        if(StringUtils.isEmpty(business.getOpeningTime())){
            business.setOpeningTime("");
        }
        if(StringUtils.isEmpty(business.getPackageFee())){
            business.setPackageFee(0.0);
        }
        if(StringUtils.isEmpty(business.getPhone())){
            business.setPhone("");
        }
        if(StringUtils.isEmpty(business.getStatus())){
            business.setStatus(0);
        }
        if(StringUtils.isEmpty(business.getShippingTime())){
            business.setShippingTime(0);
        }
        if(StringUtils.isEmpty(business.getShippingFee())){
            business.setShippingFee(0.0);
        }
        if(StringUtils.isEmpty(business.getPicUrl())){
            business.setPicUrl("");
        }
        businessDao.addBusiness(business);
    }

    /**
     * 获取所有business
     *
     * @param page page
     * @return business
     */
    public List<Business> getAll(String uid,Page page){
        List<Business> businessList =  businessDao.getAll(page);
        for (Business business : businessList) {
            Favorite favorite = favoriteDao.getByUidAndBid(business.getId(),uid);
            business.setLike(favorite == null ? false : true);
        }
        return businessList;
    }

    /**
     * 获取指定的商家
     *
     * @param id id
     * @return b
     */
    public Business get(String id){
        return businessDao.getOne(id);
    }

    /**
     * 删除指定的id
     *
     * @param id id
     */
    public void remove(String id){
        businessDao.remove(id);
    }

    /**
     * 更新指定的商家
     *
     * @param business b
     */
    public void update(Business business){
        businessDao.update(business);
    }

    /**
     * 添加喜欢
     *
     * @param favorite
     */
    public void add(Favorite favorite){
        favorite.setCreateTime(System.currentTimeMillis());
        favorite.setId(RandomUtil.getUUID());
        favoriteDao.add(favorite);
    }

    /**
     * 喜欢商家
     *
     * @param uid
     * @param bid
     */
    public void favoriteBusiness(String uid,String bid){
        Favorite favorite = favoriteDao.getByUidAndBid(bid,uid);
        if(favorite == null){
            Favorite newFavorite = new Favorite();
            newFavorite.setUserId(uid);
            newFavorite.setBusinessId(bid);
            add(newFavorite);
        } else {
            favoriteDao.removeById(favorite.getId());
        }
    }

    /**
     * 获取喜欢商家列表
     *
     * @param uid uid
     * @param page page
     * @return List<FavoriteDisplay></>
     */
    public List<FavoriteDisplay> getFavoriteDisplayList(String uid,Page page){
        List<FavoriteDisplay> result = new ArrayList<>();
        List<Favorite> favoriteList = favoriteDao.getAllByUid(uid,page);
        for (Favorite favorite : favoriteList) {
            FavoriteDisplay favoriteDisplay = new FavoriteDisplay();
            favoriteDisplay.setId(favorite.getBusinessId());
            favoriteDisplay.setBusinessInfo(businessDao.getOne(favorite.getBusinessId()));
            result.add(favoriteDisplay);
        }
        return result;
    }
}
